package priv.pront.code.national.acwing.dp.sm;

import java.util.Scanner;

public class Acwing1058_股票买卖V {

    static int N, INF = 0x3f3f3f3f;
    static int[][] dp;
    static int[] w;
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        N = scanner.nextInt();
        w = new int[N + 1];
        for(int i = 1; i <= N; i++){w[i] = scanner.nextInt();}
        dp = new int[N + 1][3];
        dp[0][0] = dp[0][1] = -INF;
        dp[0][2] = 0;
        for(int i = 1; i <= N; i++){
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - w[i]);
            dp[i][1] = dp[i - 1][0] + w[i];
            dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]);
        }
        System.out.println(Math.max(dp[N][1], dp[N][2]));
    }
}
